﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Xml;

namespace TeamFoundation.EventBus
{
    public class Action : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string tn = context.Request.Params["type"];
            if (tn == null)
            {
                context.Response.Redirect("Default.aspx");
                return;
            }
            tn = "TeamFoundation.EventBus." + tn;
            Type t = Type.GetType(tn);
            if (t == null || !typeof(IAction).IsAssignableFrom(t))
            {
                context.Response.Redirect("Default.aspx");
                return;
            }
            IAction action = (IAction)Activator.CreateInstance(t);

            context.Response.BufferOutput = false;
            context.Response.Buffer = false;

            context.Response.ContentType = "text/html";

            // Send through header.
            context.Response.Write(
@"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">
<html xmlns=""http://www.w3.org/1999/xhtml"" >
<head>
    <link rel=""Stylesheet"" type=""text/css"" href=""default.css"" />
    <title>TFEB | Installation Progress</title>
</head>
<body>
    <div id=""content"">
        <h1>TFEB | Action Progress</h1>
        <p>TFEB is making changes to your server.</p>
        <p style='font-family:monospace'>");
            context.Response.Flush();

            action.Action(context);

            context.Response.Write(
@"        </p>
    </div>
</body>
</html>");
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
